package cn.kgc.layui.dao.impl;

import cn.kgc.layui.dao.AuthorityDao;
import cn.kgc.layui.entity.Authority;
import cn.kgc.layui.entity.Student;
import cn.kgc.layui.util.DruidDBUtil;
import cn.kgc.layui.vo.ZtreeVO;
import com.alibaba.druid.util.StringUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ColumnListHandler;

import java.sql.SQLException;
import java.util.List;

public class AuthorityDaoImpl implements AuthorityDao {
    @Override
    public List<Authority> list(String authorityName,String menuUrl,String authority) {
        // 获取QueryRunner
        QueryRunner qr = new QueryRunner(DruidDBUtil.getDatasource());
        String sql="select authority_id authorityId,authority_name authorityName,menu_url menuUrl,parent_id parentId,is_menu isMenu,menu_icon menuIcon,order_number orderNumber,create_time createTime,update_time updateTime,authority from sys_authority where 1=1";


        try {
            String sqlWhere="";
            if(null!=authorityName&&(!StringUtils.isEmpty(authorityName))){
                sqlWhere+=" and authority_name like'%"+authorityName+"%'";
            }
            if(null!=menuUrl&&(!StringUtils.isEmpty(menuUrl))){
                sqlWhere+=" and menu_url like'%"+menuUrl+"%'";
            }
            if(null!=authority&&(!StringUtils.isEmpty(authority))){
                sqlWhere+=" and authority like'%"+authority+"%'";
            }
            sql=sql+sqlWhere;
            return qr.query(sql, new BeanListHandler<>(Authority.class));
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;

    }

    @Override
    public List<ZtreeVO> ztreeData() {
        // 获取QueryRunner
        QueryRunner qr = new QueryRunner(DruidDBUtil.getDatasource());
        String sql="select authority_id id,authority_name name,parent_id pId from sys_authority";


        try {

            return qr.query(sql, new BeanListHandler<>(ZtreeVO.class));
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }

    @Override
    public int save(Authority authority) throws SQLException {
        int result=0;
        try {
            // 获取QueryRunner
            QueryRunner qr = new QueryRunner(DruidDBUtil.getDatasource());
            String sql="insert into sys_authority (authority_name,menu_url,parent_id,is_menu,menu_icon,order_number,create_time,update_time,authority) values(?,?,?,?,?,?,?,?,?)";
            result=qr.update(sql,authority.getAuthorityName(),authority.getMenuUrl(),authority.getParentId(),authority.getIsMenu(),authority.getMenuIcon(),authority.getOrderNumber(),authority.getCreateTime(),authority.getUpdateTime(),authority.getAuthority());
            return result;
        }catch (Exception e){
            e.printStackTrace();
        }
        return result;
    }

    @Override
    public int update(Authority authority) throws SQLException {
        int result=0;
        try {
            // 获取QueryRunner
            QueryRunner qr = new QueryRunner(DruidDBUtil.getDatasource());
            String sql="update sys_authority set authority_name=?, menu_url=?,parent_id=?,is_menu=?,menu_icon=?,order_number=?,update_time=?,authority=? where authority_id=?";
            result=qr.update(sql,authority.getAuthorityName(),authority.getMenuUrl(),authority.getParentId(),authority.getIsMenu(),authority.getMenuIcon(),authority.getOrderNumber(),authority.getUpdateTime(),authority.getAuthority(),authority.getAuthorityId());
            return result;
        }catch (Exception e){
            e.printStackTrace();
        }
        return result;
    }

    @Override
    public int delete(String id) throws SQLException {
        int result=0;
        try {
            // 获取QueryRunner
            QueryRunner qr = new QueryRunner(DruidDBUtil.getDatasource());
            String sql="delete from sys_authority where authority_id=?";
            result=qr.update(sql,id);
            return result;
        }catch (Exception e){
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 根据角色id查询该角色拥有的所有权限
     * @param roleId
     * @return
     */
    public List<Integer> getAuthIdsByRoleId(String roleId){
        try {
            // 获取QueryRunner
            QueryRunner qr = new QueryRunner(DruidDBUtil.getDatasource());
            String sql="select auth_id from sys_role_auth where role_id=?";
            //return qr.query(sql,new BeanListHandler<>(String.class),roleId);
            return qr.query(sql,new ColumnListHandler<Integer>("auth_id"),roleId);

        }catch (Exception e){
            e.printStackTrace();
        }
        return null;

    }
}
